const api = require("../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    user:{
      memberName:'微信用户',
      memberFaceImg:'/static/img/yonghu.png'
    },
    modalName:null,
    bindPhone:false,
    phone:''
  },

  _loadAppConfig:function(){
    const _self = this;
    api.post(api.url.commonAppConfig,{
        appId:my.getAppIdSync().appId,
        type:2
    },res=>{
        if(res.data.code==200){
            _self.setData({phone:res.data.body.serverPhone})
        }
    })
  },
  
  getPhoneNumber() {
    let _self = this;
    my.getPhoneNumber({
      success: (res) => {
        console.log(res.response);
        api.post(api.url.userPhoneRegister,{
          appId:my.getAppIdSync().appId,
          memberId:_self.data.user.memberId,
          encryptedData:res.response
        },res=>{
          if(res.data.code==200){
              app.globalData.userInfo.memberId = res.data.body.memberId;
              app.globalData.userInfo.memberName = res.data.body.memberName;
              app.globalData.userInfo.memberFaceImg = res.data.body.memberFaceImg;
              app.globalData.userInfo.memberPhone = res.data.body.memberPhone;
              app.globalData.userInfo.loginToken = res.data.body.loginToken;
              app.globalData.userInfo.loginLevel = 3;
              _self.setData({
                user:app.globalData.userInfo,
                bindPhone:app.globalData.userInfo.memberPhone?false:true
              })
          }else if(res.data.msg){
            my.showModal({
              content: res.data.msg,
              showCancel:false
            })
          }else{
            my.showToast({
              content: '绑定手机失败',
              type:'error'
            })
          }
        })
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },

  toInfo(){
    my.navigateTo({
      url: '../info/info',
    })
  },

  butInterface(e){
    let flag = e.currentTarget.dataset.flag;
    let self = this;
    switch(flag){
      case 'orderList' :
        // console.log('我的订单');
        my.navigateTo({
          url: "/pages/order/list/list",
        })
        break;
      case 'coupon':
        my.navigateTo({
            url: "/pages/mine/coupon/coupon",
        })
        break;
      case 'kefu' :
        // console.log('联系客服');
        self.setData({
          modalName:'Modal',
        })
        break;
      default:
        my.showToast({
          content: '功能开发中 ...',
          icon: 'error',
          duration: 2000
        })
        break;
    }
  },

  //关闭客服模态框
  btnHideModal(e){
    this.setData({
      modalName:null,
      bindPhone:false
    })
  },

  //拨打客服电话
  butCall(){
    let self = this;
    if(self.data.phone){
      let serverPhone = self.data.phone.replace(/\s/g, "");
      console.log(serverPhone);
      my.makePhoneCall({
        number: serverPhone,
        complete(){
          self.btnHideModal();
        }
      })
    }
  },
  onLoad() {
    this.setData({
      user:app.globalData.userInfo,
      phone:app.globalData.phone,
      bindPhone:app.globalData.userInfo.memberPhone?false:true
  })
  this._loadAppConfig()
  },
});
